<template>
  <el-dialog
    :append-to-body="true"
    :close-on-click-modal="false"
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item label="旧密码" prop="oldPassword">
        <el-input
          v-model.trim="form.oldPassword"
          clearable
          placeholder="请输入旧密码"
          type="password"
        />
      </el-form-item>
      <el-form-item label="新密码" prop="newPassword">
        <el-input
          v-model.trim="form.newPassword"
          clearable
          placeholder="请输入新密码"
          type="password"
        />
      </el-form-item>
      <el-form-item label="确认密码" prop="newPasswordConfirm">
        <el-input
          v-model.trim="form.newPasswordConfirm"
          clearable
          placeholder="请再次输入密码"
          type="password"
        />
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button :loading="isSaving" type="primary" @click="save">
        确 定
      </el-button>
    </template>
  </el-dialog>
</template>

<script>
  import { changePassword } from '@/api/user'
  import crypto from 'crypto'

  export default {
    name: 'VabChangePassword',
    data() {
      const validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'))
        } else if (value !== this.form.newPassword) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      }
      return {
        isSaving: false,
        form: {
          oldPassword: '',
          newPassword: '',
          newPasswordConfirm: '',
        },
        rules: {
          oldPassword: [
            { required: true, trigger: 'blur', message: '请输入旧密码！' },
          ],
          newPassword: [
            { required: true, message: '请输入新密码', trigger: 'blur' },
            {
              min: 6,
              max: 20,
              message: '密码长度范围6-20位字符',
              trigger: 'blur',
            },
          ],
          newPasswordConfirm: [
            { required: true, message: '请再次输入密码', trigger: 'blur' },
            { validator: validatePass2, trigger: 'blur', required: true },
          ],
        },
        title: '修改密码',
        dialogFormVisible: false,
      }
    },
    computed: {
      old_password() {
        let md5 = crypto.createHash('md5')
        md5.update(this.form.oldPassword)
        let ret = md5.digest()
        md5 = crypto.createHash('md5')
        md5.update(ret)
        ret = md5.digest('hex')
        return ret
      },
      new_password() {
        let md5 = crypto.createHash('md5')
        md5.update(this.form.newPassword)
        let ret = md5.digest()
        md5 = crypto.createHash('md5')
        md5.update(ret)
        ret = md5.digest('hex')
        return ret
      },
    },
    created() {},
    methods: {
      showEdit() {
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            this.isSaving = true
            changePassword(this.old_password, this.new_password)
              .then((res) => {
                if (res && res.status === 0) {
                  this.$baseMessage(
                    '密码修改成功！',
                    'success',
                    'vab-hey-message-success'
                  )
                  this.close()
                } else {
                  this.$baseMessage(
                    res ? res.msg : '密码修改失败！',
                    'error',
                    'vab-hey-message-error'
                  )
                }
              })
              .catch((err) => {
                console.log(err)
                this.$baseMessage(
                  res ? res.msg : '操作失败，请重试！',
                  'error',
                  'vab-hey-message-error'
                )
              })
              .then(() => {
                this.isSaving = false
              })
          }
        })
      },
    },
  }
</script>
